package com.engc.common.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.engc.common.service.BaseService;
import com.engc.common.service.OrgService;
import com.engc.common.utils.Constant;

@Service
public class OrgServiceImpl implements OrgService {

	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Resource
	private BaseService baseService;

	public boolean orgHasCustomer(String orgid) {
		int count = jdbcTemplate.queryForInt("select count(1) from stb_customer where  ZHLX <> ? AND  orgid like ?", new Object[] {Constant.ACCOUNT_VITUAL_TYPE, orgid + "%" });
		return count < 1 ? false : true;
	}
	

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getOrgid(final String fatherid, final Integer areano, final Integer nodepath) {
		
		try{
			final String sql = "{? = call f_comm_getorgid(?,?,?)}";
			System.out.println(sql);
			Object obj = this.jdbcTemplate.execute(new ConnectionCallback() {
				public Object doInConnection(Connection conn) throws SQLException, DataAccessException {

					CallableStatement cstmt = conn.prepareCall(sql);

					cstmt.registerOutParameter(1, Types.VARCHAR);
					cstmt.setString(2, fatherid);
					cstmt.setInt(3, areano);
					cstmt.setInt(4, nodepath);

					cstmt.execute();
					String out = cstmt.getString(1);
					cstmt.close();

					return out;
				}
			});
			if (obj != null && !obj.toString().trim().equals("")) {
				return obj.toString();
			}
			return null;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}

		

	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public boolean orgHasChild(String orgid) {
		int count = jdbcTemplate.queryForInt("select count(1) from stb_org where status=? and orgid like ?", new Object[] {Constant.VALID_YES, orgid + "%" });
		return count <= 1 ? false : true;
	}


	@Override
	public String checkOrgIsRegister(String sql, String mc, String fatherOrgid,
			String bh) {
		if (null != bh) {
			if (baseService.getTotal(sql, new Object[] { mc, fatherOrgid,bh }) > 0) {
				return "1";
			} else {
				return "0";
			}
		}else{//增加
			if(baseService.getTotal(sql, new Object[]{mc,fatherOrgid}) > 0){
				return "1";
			}else{
				return "0";
			}
		}
	}


	@Override
	public String checkAdminIsRegister(String sql, String admin, String bh) {
		if(null != bh){
			if(baseService.getTotal(sql, new Object[]{admin,bh}) > 0){
				return "1";
			}else{
				return "0";
			}
		}else{
			if(baseService.getTotal(sql, new Object[]{admin}) > 0){
				return "1";
			}else{
				return "0";
			}
		}
	}


	@Override
	public String checkNJPXIsExist(String sql, String dataOrgid, String njpx,
			String bh) {
		if(null != bh){
			if(baseService.getTotal(sql, new Object[]{dataOrgid,njpx,bh}) > 0){
				return "1";
			}else{
				return "0";
			}
		}else{
			if(baseService.getTotal(sql, new Object[]{dataOrgid,njpx}) > 0){
				return "1";
			}else{
				return "0";
			}
		}
		
	}


	@Override
	public boolean orgHasChild2(String orgid) {
		int count = jdbcTemplate.queryForInt("select count(1) from stb_org where nodepath >=10 and status=? and orgid like ?", new Object[] {Constant.VALID_YES, orgid + "%" });
		return count <= 1 ? false : true;
	}


	@Override
	public boolean orgHasVerifyCustomer(String id) throws Exception {
		String sql = "select count(1)  from  stb_customer_entry where  orgid = ?" ;
		int count = jdbcTemplate.queryForInt(sql,id) ;
		return count >0 ? true : false;
	}
	
	

}
